package com.shop.daoImpl;

import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.shop.dao.MemberDao;
import com.shop.entity.Address;
import com.shop.entity.CarGoodsList;
import com.shop.entity.Collection;
import com.shop.entity.Footprint;
import com.shop.entity.Member;
import com.shop.entity.MemberInfor;
import com.shop.entity.Order;

@Repository
public class MemberDaoImpl extends BaseDaoImpl<Member> implements MemberDao {
	public MemberDaoImpl() {
		super(Member.class);
	}

	@Override
	public Object[] findByUsername(String username, String password) {
		Criteria c = getSession().createCriteria(Member.class);
		c.add(Restrictions.eq("username", username));
		Member member = (Member) c.uniqueResult();
		System.out.println("校对登录数据");
		if(member != null) {
			if(member.getPassword().equals(password))
				return new Object[]{"0000", member.getId()};
			else 
				return new Object[]{"0002", null};
		}
		return new Object[]{"0001", null};
	}

	@Override
	public Set<Order> findOrder(Member menber) {
		return menber.getOrder();
	}

	@Override
	public Set<Footprint> findFoot(Member menber) {
		return menber.getFootprint();
	}

	@Override
	public Set<Collection> findColle(Member menber) {
		return menber.getCollection(); 
	}

	@Override
	public Set<CarGoodsList> findCarGoods(Member menber) {
		return menber.getCarGoodsList();
	}

	@Override
	public Set<Address> findAddress(Member menber) {
		return menber.getAddress();
	}

	@Override
	public MemberInfor findMemberInfor(Member menber) {
		return menber.getInfor();
	}

	@Override
	public boolean saveOrder(Member menber, Order order) {
		menber.getOrder().add(order);
		getSession().save(menber);
		return true;
	}

	@Override
	public boolean saveColle(Member menber, Collection colle) {
		menber.getCollection().add(colle);
		getSession().save(menber);
 		return true; 
	}

	@Override
	public boolean saveFoot(Member menber, Footprint foot) {
		menber.getFootprint().add(foot);
		getSession().save(menber);
 		return true; 
	}

	@Override
	public boolean saveCarGoodsList(Member menber, CarGoodsList gl) {
		menber.getCarGoodsList().add(gl);
		getSession().save(menber);
 		return true; 
	}

	@Override
	public boolean saveAddress(Member menber, Address adds) {
		menber.getAddress().add(adds);
		getSession().save(menber);
 		return true; 
	}

	@Override
	public boolean saveInfor(Member menber, MemberInfor minfor) {
		menber.setInfor(minfor);
		getSession().save(menber);
		getSession().save(minfor);
 		return true; 
	}
}
